<?php
namespace app\mypay\controller;
use think\Controller;
use think\Db;
use think\Loader;
class Weixinpay extends Controller
{
    
    public function _initialize()
    {
        $config = cache('db_config_data');
        if (!$config)
        {
            $config = api('Config/lists');
            cache('db_config_data', $config);
        }
        config($config);
    }
    public function _empty($name)
    {
        return $this->fetch('/Public/404');
    }
     
    public function newweixinpay()
    {
        
        Loader::import('weixinpay.WxPayPubHelper');
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false)
        {
        	exit('请在微信里面打开我啦~~');
        }
        else
        {
            
            $state    = input('state');
            $openid   = input('openid');
            
            if(empty($state) || empty($openid)){
               
              
		        die('参数错误2！');
		    }
        	$order_info = Db::name('order')->where(['orderid' => $state])->find();
	
    		if(!$order_info){
    		      die('订单存在！');
    		}
          
            
         
            
            
            
            $out           = $order_info['orderid'];
            $total_fee     = $order_info['money']*100;
            $codehou       = $order_info['code'];
            $openid        = $openid;
            $userid        = $order_info['userid'];
            $ddh3          = $order_info['ddh3'];
            $uopenid       = $order_info['uopenid'];
            $zyid          = $order_info['zyid'];
            
            $ip    = getIP();
            check_ts($ip,$uopenid);
            $zftdu = Db::name('member')->where(['id' => $userid])->value('zftd');
            
            if($zftdu != 1){
                 die('暂无商户？');
            }
            
            
            if(empty($out) || empty($total_fee) || empty($codehou) || empty($openid) || empty($userid) || empty($ddh3) || empty($uopenid)){
             
             
            
             
                 die('兄弟你干啥呢1？');
            
            }
        
             if(!is_numeric($userid) ||  $userid == 0 || $total_fee == 0){
                
                 die('兄弟你干啥呢2？');
                
            }
            
             if(!preg_match("/^\d+(\.\d+)?$/",$total_fee)){
            
                 die('兄弟你干啥呢3？');
                
            }
            
           
            $zhifu = Db::name("zhifu")->where(['id' => 1])->find();
            $content = unserialize($zhifu['content']);
            $jsApi = new \JsApi_pub();
            
           
            
            
            $xieyi = is_HTTPS()?"https://":"http://";
           
            $timeStamp = time();
            $out_trade_no = $content['appid'] . random(4) . "$timeStamp";
            $body = config("zhifuxianshi");
            $unifiedOrder = new \UnifiedOrder_pub();
       
   
            $unifiedOrder->setParameter("openid", "$openid"); //商品描述
            $unifiedOrder->setParameter("body", "$body"); //商品描述
         
            $unifiedOrder->setParameter("out_trade_no", "$out"); //商户订单号 
            $unifiedOrder->setParameter("total_fee", intval($total_fee)); //总金额
            $unifiedOrder->setParameter("notify_url", $xieyi . $content['shename'].'/mypay/huidiao/wxnotify'); //通知地址 
            $unifiedOrder->setParameter("trade_type", "JSAPI"); //交易类型
 
            $unifiedOrder->setParameter("attach", $body); //附加数据
            
         
        
           
            $prepay_id = $unifiedOrder->getPrepayId();
            $jsApi->setPrepayId($prepay_id);
           
            $jsApiParameters = $jsApi->getParameters();
            $this->assign("jsApiParameters", $jsApiParameters);
           
     
            $listsss = Db::name('ename')->where(['status' => 1, 'zt' => 1,'type' => 3])->limit(0,2)->select();

            $ename = array_rand($listsss,1);
            $ename = $listsss[$ename]['ename'];
            
            Db::name('ename')->where(['ename' => $ename])->setInc('count',1);
          
            $tzurl  = "http://" . $ename."/index/index/hezi/?userid=".$userid."&ddh=".$ddh3."&uopenid=".$uopenid;
  
            $tzurls  = "http://" . $ename."/index/index/shipinok/?zyid=".$zyid."&ddh=".$out."&uopenid=".$uopenid;
          
            $this->assign("userid", $userid);
            
            $this->assign("tzurls", $tzurls);
            $this->assign("tzurl", $tzurl);
           
            return $this->fetch();
          
        }
    }
    
   
    
}